package com.yyfax.common.form.interaction;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.yyfax.common.R;
import com.yyfax.common.animation.ResizeWidthAnimation;
import com.yyfax.common.form.SearchBar;
import com.yyfax.common.form.adapter.SearchTypeAdapter;
import com.yyfax.common.tools.DensityUtil;

import java.util.List;


/**
 * Created by fan on 2017/4/1.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class ComplexTypeInteraction extends SearchBarInteraction implements View.OnClickListener,
		TextView.OnEditorActionListener,
		SearchTypeAdapter.DetailItemClickListener {

	private final static int LEFT = 0;
	private final static int RIGHT = 1;
	private final int mPopupWindowTopMagin;

	private EditText mSearchContentEt;
	private TextView mSearchTypeBtn;
	private View msearchContainer;
	private int mDirection = RIGHT;
	private int mSpace = 0;//默认客户姓名
	private SearchTypeAdapter mPopupListAdapter;
	private PopupWindow mPopupWindow;
	private int mType;
	private String mSearchContent;
	private View mSearchBar;
	private int mOverdueType = SearchBar.A_MONTH;

	public ComplexTypeInteraction(Context context, TypedArray ta, View view) {
		super(context, ta, view);
		mSpace = DensityUtil.dip2px(context, 49);
		mPopupWindowTopMagin = DensityUtil.dip2px(context, 10);
	}

	@Override
	protected void initView(Context context, TypedArray ta, View view) {
		mSearchContentEt = (EditText) view.findViewById(R.id.search_content_et);
		msearchContainer = view.findViewById(R.id.search_container);
		mSearchTypeBtn = (TextView) view.findViewById(R.id.search_type_btn);
		View cancelBtn = view.findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(this);
		mSearchContentEt.setOnClickListener(this);
		mSearchContentEt.setOnEditorActionListener(this);
		msearchContainer.setOnClickListener(this);
		mSearchTypeBtn.setOnClickListener(this);

		View statusContentView = View.inflate(context, R.layout.select_types_layout, null);
		mPopupListAdapter = new SearchTypeAdapter(context, this);
		((ListView) statusContentView.findViewById(R.id.select_status_lv)).setAdapter(mPopupListAdapter);
		mPopupWindow = new PopupWindow(statusContentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);
		//设置popupWindow,当点击popupWindow外面的时候可以消失
		mPopupWindow.setOutsideTouchable(true);
		mSearchBar = view.findViewById(R.id.search_bar);

	}

	@Override
	public void onItemClick(int position, String selectStatus) {
		dismissPopupWindow();
		mType = position;
		mSearchTypeBtn.setText(mPopupListAdapter.getTypes().get(position));
		mPopupListAdapter.setSelectedType(mPopupListAdapter.getTypes().get(position));
		if (selectStatus.contains("身份证")||selectStatus.contains("借据号")) {
			mSearchContentEt.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
		} else if (selectStatus.contains("日")||selectStatus.contains("天数")) {
			mSearchContentEt.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		} else {
			mSearchContentEt.setInputType(InputType.TYPE_CLASS_TEXT);
		}

		if (selectStatus.contains("客户姓名")) {
			mSearchContentEt.setHint("请输入要搜索的" + selectStatus);
		} else if (selectStatus.contains("号")) {
			mSearchContentEt.setHint("请输入客户" + selectStatus);
		} else if (selectStatus.contains("人")) {
			mSearchContentEt.setHint("请输入" + selectStatus + "姓名");
		} else if (selectStatus.contains("还款日")) {
			mSearchContentEt.setHint("请输入0-30，0即还款日为当天");
		} else if (selectStatus.contains("逾期")) {
			if (mOverdueType == SearchBar.TWO_MONTHS) {
				mSearchContentEt.setHint("请输入31-65的数字");
			} else {
				mSearchContentEt.setHint("请输入0-30的数字");
			}
		}
	}

	@Override
	public void setOverdueType(int type) {
		mOverdueType = type;
	}

	@Override
	public void setSearchTypes(List<String> types) {
		mPopupListAdapter.setTypes(types);
	}

	@Override
	public void setSearchContent(int searchType, String content) {
		mType = searchType;
		mSearchTypeBtn.setText(mPopupListAdapter.getTypes().get(searchType));
		mPopupListAdapter.setSelectedType(mPopupListAdapter.getTypes().get(searchType));
		mSearchContent = content;
		mSearchContentEt.setText(content);
	}

	@Override
	public void returnNormalStatus() {
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
			String criteria = mSearchContentEt.getText().toString().trim();
			if (TextUtils.isEmpty(criteria)) {
				Toast.makeText(mContext, R.string.search_criteria_empty, Toast.LENGTH_SHORT).show();
				return false;
			}
			mSearchContent = criteria;
			setOriginalState();
			if (mSearchContentListener != null) {
				mSearchContentListener.searchContent(mType, criteria);
			}
			return true;
		}
		return false;
	}

	@Override
	protected void setListenerToShadowView() {
		mShadowView.setOnClickListener(this);
	}

	private void setOriginalState() {
		mSearchContentEt.setText(mSearchContent);
		resetShadowState(false);
		dismissPopupWindow();
		hideInputWindow(mSearchContentEt);
		mSearchContentEt.clearFocus();
		mSearchContentEt.setFocusable(false);
	}

	private boolean isSearchState() {
		return !TextUtils.isEmpty(mSearchContent);
	}

	private void setSearchState(boolean isSearchState) {
		mSearchContent = isSearchState ? mSearchContent : "";
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.search_container || id == R.id.search_content_et) {
			if (mDirection == RIGHT) {
				startAnimation(LEFT);
			}
			resetShadowState(true);
			showInputWindow(mSearchContentEt);
			dismissPopupWindow();
		} else if (mShadowView != null && id == mShadowView.getId()) {
			if (!isSearchState()) {
				startAnimation(RIGHT);
			}
			setOriginalState();
		} else if (id == R.id.cancel_btn) {
			if (isSearchState()) {
				if (mSearchContentListener != null) {
					mSearchContentListener.searchContent(0, "");
				}
			}
			setSearchState(false);
			startAnimation(RIGHT);
			setOriginalState();
		} else if (id == R.id.search_type_btn) {
			if (mDirection == RIGHT) {
				startAnimation(LEFT);
			}
			resetShadowState(true);
			if (!mPopupWindow.isShowing()) {
				mPopupWindow.showAsDropDown(mSearchBar, 0, mPopupWindowTopMagin);
//				mPopupWindow.showAtLocation(mSearchBar, Gravity.NO_GRAVITY,0,mPopupWindowTopMagin);
				hideInputWindow(mSearchContentEt);
			} else {
				dismissPopupWindow();
			}
		}

	}

	private void dismissPopupWindow() {
		if (null != mPopupWindow && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
	}

	private void startAnimation(int direction) {
		mDirection = direction;
		ResizeWidthAnimation anim = new ResizeWidthAnimation(msearchContainer, direction == LEFT ? mSpace : 0 - mSpace);
		anim.setDuration(300);
		msearchContainer.startAnimation(anim);
	}

}
